NhapPhieuThuPanel = Ext.extend(AbstractPanel, {
	layout : {
        type : 'vbox',
        align : 'stretch'
    },
    
    height : 900,
    justUseDefaultHotKeys : false,
    
    initComponent : function() {
    	
    	this.formLabel = new Ext.form.Label({
			cls : 'main-title',
			text : lang.text.menu.nhapPt.toUpperCase()
		});
		
		this.formLabelContainer = new Ext.Container({
			cls : 'field-align-center',
			items : [this.formLabel]
		});
		
		this.hdFieldSet = new HopDongInfoFieldSet({
			readOnly : true,
			height : 180
		});
		this.ptFieldSet = this.initPhieuThuInfoFieldSet();
		this.buttonBar = this.initButtonBar();
		
		this.items = [this.formLabelContainer, {xtype : 'spacer', height : 10},
		              this.hdFieldSet, {xtype : 'spacer', height : 20},
					  this.ptFieldSet, {xtype : 'spacer', height : 20}, this.buttonBar];
		NhapPhieuThuPanel.superclass.initComponent.call(this);
    },
    
    initPhieuThuInfoFieldSet : function() {
    	var fieldPlugin = new CustomizedFieldPlugin();
    	this.soTienThuTextField = new Ext.ux.NumericField({
    		name : "soTien",
			width : 180,
			fieldLabel : lang.text.pt.soTienThu,
			autoCreate: {
	            tag: 'input', type: 'text', size: '20',
	            autocomplete: 'off', maxlength: '12'},
			useThousandSeparator : true,
			allowNegative : false,
			selectOnFocus : true,
			unit : tt_default.hd.unit,
			allowBlank : false,
			mandatory : true,
			plugins : fieldPlugin
    	}); 
    	
    	this.ngayThuDateField = new ThienTanDateField({
    		autoCreate: {
	            tag: 'input', type: 'text', size: '10',
	            autocomplete: 'off', maxlength: '6'},
			fieldLabel : lang.text.pt.ngayThu,
			width : 180,
			name: 'ngayLap',
			allowBlank : false,
			mandatory : true
		});
    	
    	var leftCtn = new Ext.Container({
    		width : 393,
			layout : 'customizedlayout',
			labelWidth : 120,
			items : [this.soTienThuTextField, this.ngayThuDateField]
		});
    	
    	this.ghiChuTextArea = new Ext.form.TextArea({
    		name : 'ghiChu',
    		width : 250,
    		fieldLabel : lang.text.ghiChu
    	});
    	
    	var rightCtn = new Ext.Container({
			layout : 'customizedlayout',
			labelWidth : 70,
			items : [this.ghiChuTextArea]
		});
    	
    	return new Ext.form.FieldSet({
			title : lang.text.pt.ptInfo,
			autoHeight : true,
			height : 110,
			layout : 'column',
			items : [leftCtn, {xtype : 'spacer', width : 20}, rightCtn]
		});
    }, 
    
    initButtonBar : function() {
    	this.luuBtn = new Ext.Button({
			width : 130,
			text : lang.text.luuTaoMoi
		});
		
		this.traHangBtn = new Ext.Button({
			width : 80,
			text : lang.text.pt.traHang
		});
		
		this.denHangBtn = new Ext.Button({
			width : 80,
			text : lang.text.pt.denHang
		});
		
		this.chiTietPtBtn = new Ext.Button({
			width : 150,
			text : lang.text.pt.chiTietPt
		});
		
		this.importBtn = new Ext.Button({
			width : 200,
			text : lang.text.pt.importFromFile
		});
		
		this.thoatBtn = new Ext.Button({
			width : 40,
			text : lang.text.thoat
		});
		
		return new Ext.Container({
			layout : 'hbox',
			items : [this.luuBtn, {xtype : 'spacer', width : 13}, 
			         this.traHangBtn, {xtype : 'spacer', width : 13}, 
			         this.denHangBtn, {xtype : 'spacer', width : 13}, 
			         this.chiTietPtBtn, {xtype : 'spacer', width : 13},
			         this.importBtn, {xtype : 'spacer', width : 13}, 
			         this.thoatBtn]
		});
    },
    
    afterRender : function() {
    	if (ApplicationManager.callbackMsg) {
    		this.formMessage.showMessages(
                    [FormMessageType.INFO], [ApplicationManager.callbackMsg.msg]);
    		this.formMessage.setVisible(true);
		}
    	this.submitButtons = [this.luuBtn, this.traHangBtn, this.denHangBtn, this.chiTietPtBtn];
    	this.traHangBtn.on('click', this.openSuaHdTraHang, this);
    	this.denHangBtn.on('click', this.openSuaHdDenHang, this);
    	this.chiTietPtBtn.on('click', this.openChiTietPt, this);
    	this.importBtn.on('click', this.importData, this);
    	this.soTienThuTextField.on('change', this.validateTienThu, this);
    	this.disableComponents();
    	NhapPhieuThuPanel.superclass.afterRender.call(this);
    },
    
    initHotKey : function() {
    	var defaultHotKeys = this.defaultHotKeys();
    	this.keys = new Ext.KeyMap(this.el, [defaultHotKeys[0], defaultHotKeys[1], {
	        key: 't',
	        ctrl : true,
	        alt : true,
	        fn: this.openSuaHdTraHang,
	        scope: this
	    }, {
	        key: 'd',
	        ctrl : true,
	        alt : true,
	        fn: this.openSuaHdDenHang,
	        scope: this
	    }, {
	        key: 'c',
	        ctrl : true,
	        alt : true,
	        fn: this.openChiTietPt,
	        scope: this
	    }]);
    },
    
    openSuaHdTraHang : function(){
    	if (!this.traHangBtn.disabled) {
    		ApplicationManager.traHangFrom = MenuItemId.nhapPT;
    		ApplicationManager.openMenu(MenuItemId.suaHDTraHang);	
		}
    },

    openSuaHdDenHang : function(){
    	if (!this.denHangBtn.disabled) {
    		ApplicationManager.denHangFrom = MenuItemId.nhapPT;
    		ApplicationManager.openMenu(MenuItemId.suaHDDenHang);	
		}
    	
    },

    openChiTietPt : function(){
    	var params = {
    			'id' : this.hdFieldSet.idHidden.getValue(),
    			'maHd' : this.hdFieldSet.maHdTextField.getValue()
    	};
        var url = "phieuThu/export?"+ Ext.urlEncode(params);
        Utility.processExportWaiting(url);
    },
    
    importData : function() {
    	var uploadCtn = new AbstractPanel({
    		autoHeight : true,
    		layout : 'customizedlayout',
    		fileUpload : true, 
    		bodyStyle: 'padding: 5px 5px 5px 5px;',
            labelWidth: 0,
    		items : [{
    			xtype : 'spacer', height : 5
    		}, new Ext.form.Label({
    			text : lang.msg.selectFile,
    			cls : 'x-form-item'
    		}), {
    			xtype : 'spacer', height : 15
    		}, new Ext.form.TextField({
				name: 'fileUpload',
				id: 'fileUpload',
				inputType: 'file',
				width: 300
			})],
            buttons : [{
            	text: lang.text.proceed,
            	handler : function() {
            		var fileName = uploadCtn.items.items[4].getValue();
            		var extension = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length);
            		if (extension == tt_default.xls || extension == tt_default.xlsx) {
            			if(uploadCtn.getForm().isValid()){
                			uploadCtn.getForm().submit({
                				url: 'phieuThu/importData',
                				waitMsg: lang.text.loading,
                				success: function(action, response){
                					var result = Ext.decode(response.response.responseText);
                					uploadCtn.ownerCt.close();
                					this.notifyMessage(result.msg);
                					this.addMessages(FormMessageType.ERROR, [result.error]);
                					this.addMessages(FormMessageType.INFO, [result.updated]);
                				},
                				failure : function(action, response){
                					var result = Ext.decode(response.response.responseText);
                					uploadCtn.ownerCt.close();
                					this.notifyMessage(result.msg);
                				},
                				scope : this
                			});
                			
            			}
            		} else if(fileName == "" || fileName == null){
            			uploadCtn.notifyMessage({action : 'error', msg : lang.msg.selectFileFirst})
					} else {
            			uploadCtn.notifyMessage({action : 'error', msg : lang.msg.unsupportedFormat})
					} 
            	},
            	scope : this
            }, {
            	text: lang.text.cancel,
		        handler: function(){
		        	uploadCtn.ownerCt.close();
		        },
		        scope : uploadCtn
            }]
    	});
    	var win = new Ext.Window({
    		modal : true,
    		resizable : false,
    		draggable : false,
    		closable : false,
    		width : 500,
    		title : lang.text.pt.importFromFile,
    		height : 150,
    		autoHeight : true,
    		autoScroll : true,
    		items : [uploadCtn]
       });
       win.show();
    },
    
    validateTienThu : function(field, newVal, oldVal) {
    	if (this.destroying) {
			return
		}
    	field.clearInvalid();
    	if (field.disabled) {
			return false;
		}
    	var hdId = this.hdFieldSet.idHidden.getValue();
    	if(hdId == null || hdId == "") {
    		return false;
    	}
    	if(newVal == 0 || newVal * 1000 > parseFloat(this.hdFieldSet.tienConLaiTextField.getValue().replace(/,/gi, ''))) {
    		field.markInvalid({id : 'soTien', action : 'error', msg : lang.msg.validateSoTienThu})
    		return false;
    	}
    	return true;
    },
    
    save : function() {
    	if (!this.isSubmittable()) {
			this.notifyMessage({action : 'error', msg : lang.msg.responseClientInvalidError});
			return;
		}
    	
    	this.disableButtons();
    	this.soTienThuTextField.setValue(this.soTienThuTextField.getValue());
    	this.getForm().submit({
		    me : this,
		    url : 'phieuThu/savePhieuThu',
            waitMsg: lang.text.saving,
            waitMsgTarget : true,
            success : function(opts, response) {
            	this.enableButtons();
            	var result = Ext.decode(response.response.responseText);
            	if (result.success) {
					this.notifyMessage(result.msg);
					var tienDaThu = Utility.getRawValueOfNumericField(this.hdFieldSet.tienDaThuTextField);
					var tienConLai = Utility.getRawValueOfNumericField(this.hdFieldSet.tienConLaiTextField);
					ApplicationManager.hdInfo.tienDaThu = tienDaThu + this.soTienThuTextField.getValue() * 1000;
					ApplicationManager.hdInfo.tienConLai = tienConLai - this.soTienThuTextField.getValue() * 1000;
					this.hdFieldSet.tienDaThuTextField.setValue(Utility.formatThousandSeparator(tienDaThu + this.soTienThuTextField.getValue() * 1000));
					this.hdFieldSet.tienConLaiTextField.setValue(Utility.formatThousandSeparator(tienConLai - this.soTienThuTextField.getValue() * 1000));
					if (tienConLai - this.soTienThuTextField.getValue() == 0) {
						this.hdFieldSet.trangThai.setValue(lang.text.hd.ketThuc);
					}
				} else {
					this.notifyMessage(result.msg);
				}
            	this.hdFieldSet.maHdTextField.focus(true);
            },
            failure : function(opts, response){
            	this.enableButtons();
            	this.hdFieldSet.maHdTextField.focus(true);
            }, 
            scope : this
    	});
    },
	
	defaultActionHandler : function(){
		if(!this.ghiChuTextArea.hasFocus && !this.hdFieldSet.maHdTextField.hasFocus 
				&& this.validateTienThu(this.soTienThuTextField, this.soTienThuTextField.getValue())) {
			this.save();
		}
	},
	
	beforeRequestHdInfo : function() {
    	this.ngayThuDateField.reset();
		this.soTienThuTextField.reset();
		this.ghiChuTextArea.reset();
		this.disableComponents();
	},
	
	afterRequestHdInfo : function() {
		if (this.hdFieldSet.trangThai.getValue() == lang.text.hd.dangGop) {
			this.soTienThuTextField.enable();
			this.ngayThuDateField.enable();
			this.ghiChuTextArea.enable();
			this.luuBtn.enable();
			this.denHangBtn.enable();
			this.traHangBtn.enable();
			this.chiTietPtBtn.enable();
			this.ngayThuDateField.setValue(new Date());
			this.soTienThuTextField.setValue(tt_default.hd.thuMoiThang);
			this.soTienThuTextField.focus(true);
		} else {
			this.chiTietPtBtn.enable();
		}
	},
	
	disableComponents : function() {
		this.soTienThuTextField.disable();
		this.ngayThuDateField.disable();
		this.ghiChuTextArea.disable();
		this.luuBtn.disable();
		this.denHangBtn.disable();
		this.traHangBtn.disable();
		this.chiTietPtBtn.disable();
	}
});